package com.dongdongshop.controller;

import com.dongdongshop.data.PageBean;
import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.service.ContentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {

    @DubboReference
    private ContentService contentService;

    @RequestMapping("getContentCateGory")
    @ResponseBody
    public Result getContentCateGory(String name){
        try {
            List<TbContentCategory> list = contentService.getContentCateGory(name);
            return Result.respones(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("insertContent")
    @ResponseBody
    public Result insertContent(TbContentCategory contentCategory){
        try {
            contentService.insertContent(contentCategory);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("getContent")
    @ResponseBody
    public Result getContent(Long id){
        try {
            TbContentCategory contentCategory = contentService.getContent(id);
            return Result.respones(ResultEnum.SUCCESS).setData(contentCategory);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("updateContent")
    @ResponseBody
    public Result updateContent(TbContentCategory tbContentCategory){
        try {
            contentService.updateContent(tbContentCategory);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("deleteContent")
    @ResponseBody
    public Result deleteContent(@RequestParam("id[]") Long[] id){
        try {
            contentService.deleteContent(id);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("getContentList")
    @ResponseBody
    public Result getContentList(@RequestParam(defaultValue = "1")Integer pageNumber,@RequestParam(defaultValue = "5")Integer pageSize){
        try{
            PageBean<TbContent> page = contentService.getContentList(pageNumber,pageSize);
            return Result.ok().setData(page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("insertContents")
    @ResponseBody
    public Result insertContents(TbContent content){
        try{
            contentService.insertContents(content);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.respones(ResultEnum.ERROR);
        }
    }


    @RequestMapping("getContentById")
    @ResponseBody
    public Result getContentById(Long id){
        try{
            TbContent content = contentService.getContentById(id);
            return Result.ok().setData(content);
        }catch (Exception e){
            e.printStackTrace();
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("updateContents")
    @ResponseBody
    public Result updateContents(TbContent content){
        try{
            contentService.updateContents(content);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.respones(ResultEnum.ERROR);
        }
    }
}
